<?php

include_once("interface.php");

class DNS extends Data
{
    private $fn = "/etc/resolv.conf";
    public $title = "DNS Configuration";
    public $module = "dns";
    public $data;

    public function __construct() {
        $this->data = array();
    }

    //read the data from files and return a complete object
    function readDataFromSystem() {
        $filearray = file($this->fn);
        if ( $filearray == false ) {
            return false;
        }

        foreach ( $filearray as $arrval ) {
            if ( strstr($arrval, "nameserver") ) {
                list($val1, $val2) = split(" ", $arrval);
                $dns = trim($val2);
                array_push($this->data, $dns);
            }
        }

        error_log("DNS readDataFromSystem(): read from $this->fn");
        return $this->data;
    }

    //validate data on the server side
    function validateOnServer($obj) {
        foreach ( $obj as $ip ) {
            if( ! filter_var($ip, FILTER_VALIDATE_IP) ) {
                return "Invalid DNS entry $ip";
            }
        }
        return null;
    }

    //write the obj to the file
    function commitDataToSystem($obj) {
        $ret = 0;
        $retstr = "Successfully set DNS entries";

        $vstr = $this->validateOnServer($obj);
        if($vstr) {
            return "\"$vstr\"";
        }

        error_log("DNS commitDataToSystem(): writing to $this->fn");
        $fp = fopen($this->fn, 'w');
        if($fp) {
            foreach ( $obj as $o ) {
                $str = "nameserver ".$o."\n";
                $ret = fwrite($fp, $str);
                if($ret === false) {
                    $retstr = "Failed to set DNS entries. Cannot write to ".$this->fn;
                    error_log("$retstr\n");
                    break;
                }
            }
            fclose($fp);

            exec("/etc/init.d/network restart", &$out, &$ret);
            if($ret != 0) {
                $retstr = "Failed to set DNS";
                error_log("$retstr\n");
            }
        }
        else {
            $retstr = "Failed to set DNS entries. Cannot open ".$this->fn;
        }

        return $retstr;
    }

    function printModuleHTML() {
        $str = <<<END

    <form>
    <fieldset class="ui-widget-content">
    <legend>DNS Servers</legend>

    DNS 1:&nbsp;<input type="text" size=15 id="dns0"/><br>
    DNS 2:&nbsp;<input type="text" size=15 id="dns1"/><br>
    DNS 3:&nbsp;<input type="text" size=15 id="dns2"/><br>
    <br>
    <input type="button" id="submit" value="Submit"/>
    </fieldset>
    </form>
    <div id="info"></div>

END;

        return $str;
    }

    function printModuleScript() {
        $str = <<<END

<script type="text/javascript">
    //fill in the form using values received from server
    function writeDataToHTML(data) {
        for ( i = 0; i < data.length; i++ ) {
            var dns_id = "#dns" + i;
            $(dns_id).val(data[i]);
        }
    }

    //fill in the obj to be sent to the server using the form values
    function readDataFromHTML() {
        var data = Array();
        for ( i = 0; i < 3; i++ ) {
            var dns_id = "#dns" + i;
            var val = $(dns_id).val();

            if ( val === "" ) { 
                continue;
            }
            data.push(val);
        }
        return data;
    }

    function validate(obj) {
        var msg = null;
        return msg;
    }

    //called before the submit operation
    function beforeSendFunction(msg) {
        $("#info").html("Sending ... ");
    }

    //called when the submit operation succeeds
    function successFunction(msg) {
        $("#info").html("Success: " + msg);
    }

    //called when the submit operation fails
    function errorFunction(msg) {
        $("#info").html("Error: " + msg);
    }

</script>

END;
        return $str;
    }

}

?>
